import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Student {
    private int grade;
    private String name;

    public Student() {
    }

    public Student(String name, int grade) {
        this.name = name;
        this.grade = grade;
    }

    public int getGrade() {
        return grade;
    }

    public String getName() {
        return name;
    }

    public String toString() {
        return getName() + " " + getGrade();
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = Integer.parseInt(in.next());
            int flag = Integer.parseInt(in.next());
            System.out.println(flag);
            List<Student> list = Stream
                    .of(new Student[n]).map(i -> new Student(in.next(), Integer.parseInt(in.next())))
                    .collect(Collectors.toList());
            String str = list
                    .stream()
                    .sorted(flag == 0 ? Comparator.comparing(Student::getGrade).reversed() : Comparator.comparing(Student::getGrade))
                    .map(Student::toString)
                    .collect(Collectors.joining("\n"));
            System.out.println(str);
        }
    }


}
